﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using HslCommunication.LogNet;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// Udp服务器程序的基础类，提供了启动服务器的基本实现，方便后续的扩展操作。<br />
	/// The basic class of the udp server program provides the basic implementation of starting the server to facilitate subsequent expansion operations.
	/// </summary>
	// Token: 0x0200013D RID: 317
	public class NetworkUdpServerBase : NetworkXBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06001B87 RID: 7047 RVA: 0x00088F7B File Offset: 0x0008717B
		public NetworkUdpServerBase()
		{
			this.IsStarted = false;
			this.Port = 0;
		}

		/// <summary>
		/// 服务器引擎是否启动<br />
		/// Whether the server engine is started
		/// </summary>
		// Token: 0x17000576 RID: 1398
		// (get) Token: 0x06001B88 RID: 7048 RVA: 0x00088F95 File Offset: 0x00087195
		// (set) Token: 0x06001B89 RID: 7049 RVA: 0x00088F9D File Offset: 0x0008719D
		public bool IsStarted { get; protected set; }

		/// <summary>
		/// 获取或设置服务器的端口号，如果是设置，需要在服务器启动前设置完成，才能生效。<br />
		/// Gets or sets the port number of the server. If it is set, it needs to be set before the server starts to take effect.
		/// </summary>
		/// <remarks>需要在服务器启动之前设置为有效</remarks>
		// Token: 0x17000577 RID: 1399
		// (get) Token: 0x06001B8A RID: 7050 RVA: 0x00088FA6 File Offset: 0x000871A6
		// (set) Token: 0x06001B8B RID: 7051 RVA: 0x00088FAE File Offset: 0x000871AE
		public int Port { get; set; }

		/// <summary>
		/// 后台接收数据的线程
		/// </summary>
		// Token: 0x06001B8C RID: 7052 RVA: 0x00088FB8 File Offset: 0x000871B8
		protected virtual void ThreadReceiveCycle()
		{
			IPEndPoint ipendPoint = new IPEndPoint(IPAddress.Any, 0);
			EndPoint endPoint = ipendPoint;
			while (this.IsStarted)
			{
				byte[] buffer = new byte[1024];
				try
				{
					int num = this.CoreSocket.ReceiveFrom(buffer, ref endPoint);
				}
				catch (Exception ex)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteException("ThreadReceiveCycle", ex);
					}
				}
				Console.WriteLine(DateTime.Now.ToString() + " :ReceiveData");
			}
		}

		/// <summary>
		/// 当客户端的socket登录的时候额外检查的操作，并返回操作的结果信息。<br />
		/// The operation is additionally checked when the client's socket logs in, and the result information of the operation is returned.
		/// </summary>
		/// <param name="socket">套接字</param>
		/// <param name="endPoint">终结点</param>
		/// <returns>验证的结果</returns>
		// Token: 0x06001B8D RID: 7053 RVA: 0x0000A76D File Offset: 0x0000896D
		protected virtual OperateResult SocketAcceptExtraCheck(Socket socket, IPEndPoint endPoint)
		{
			return OperateResult.CreateSuccessResult();
		}

		/// <summary>
		/// 服务器启动时额外的初始化信息，可以用于启动一些额外的服务的操作。<br />
		/// The extra initialization information when the server starts can be used to start some additional service operations.
		/// </summary>
		/// <remarks>需要在派生类中重写</remarks>
		// Token: 0x06001B8E RID: 7054 RVA: 0x00009AA1 File Offset: 0x00007CA1
		protected virtual void StartInitialization()
		{
		}

		/// <summary>
		/// 指定端口号来启动服务器的引擎<br />
		/// Specify the port number to start the server's engine
		/// </summary>
		/// <param name="port">指定一个端口号</param>
		// Token: 0x06001B8F RID: 7055 RVA: 0x00089054 File Offset: 0x00087254
		public virtual void ServerStart(int port)
		{
			bool flag = !this.IsStarted;
			if (flag)
			{
				this.StartInitialization();
				this.CoreSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
				this.CoreSocket.Bind(new IPEndPoint(IPAddress.Any, port));
				this.threadReceive = new Thread(new ThreadStart(this.ThreadReceiveCycle))
				{
					IsBackground = true
				};
				this.threadReceive.Start();
				this.IsStarted = true;
				this.Port = port;
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteNewLine();
				}
				ILogNet logNet2 = base.LogNet;
				if (logNet2 != null)
				{
					logNet2.WriteInfo(this.ToString(), StringResources.Language.NetEngineStart);
				}
			}
		}

		/// <summary>
		/// 使用已经配置好的端口启动服务器的引擎<br />
		/// Use the configured port to start the server's engine
		/// </summary>
		// Token: 0x06001B90 RID: 7056 RVA: 0x00089111 File Offset: 0x00087311
		public void ServerStart()
		{
			this.ServerStart(this.Port);
		}

		/// <summary>
		/// 服务器关闭的时候需要做的事情<br />
		/// Things to do when the server is down
		/// </summary>
		// Token: 0x06001B91 RID: 7057 RVA: 0x00009AA1 File Offset: 0x00007CA1
		protected virtual void CloseAction()
		{
		}

		/// <summary>
		/// 关闭服务器的引擎<br />
		/// Shut down the server's engine
		/// </summary>
		// Token: 0x06001B92 RID: 7058 RVA: 0x00089120 File Offset: 0x00087320
		public virtual void ServerClose()
		{
			bool isStarted = this.IsStarted;
			if (isStarted)
			{
				this.CloseAction();
				Socket coreSocket = this.CoreSocket;
				if (coreSocket != null)
				{
					coreSocket.Close();
				}
				this.IsStarted = false;
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteInfo(this.ToString(), StringResources.Language.NetEngineClose);
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x06001B93 RID: 7059 RVA: 0x0008917D File Offset: 0x0008737D
		public override string ToString()
		{
			return string.Format("NetworkUdpServerBase[{0}]", this.Port);
		}

		// Token: 0x04000608 RID: 1544
		private Thread threadReceive;
	}
}
